package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Comment;
import java.util.List;
import com.njit.WeStudy.dataobject.CommentView;
import com.njit.WeStudy.dataobject.CommentViewPlus;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface CommentMapper {
    @Delete({
        "delete from comment",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into comment (user_id, ",
        "invitation_id, storey_id, target_type,",
        "target_user_id, comment_time, ",
        "content,support_number)",
        "values (#{userId,jdbcType=INTEGER}, ",
        "#{invitationId,jdbcType=INTEGER}, #{storeyId,jdbcType=INTEGER}, ",
        "#{targetType,jdbcType=VARCHAR},#{targetUserId,jdbcType=INTEGER}, now(), ",
        "#{content,jdbcType=VARCHAR},0)"
    })
    int insert(Comment record);

    @Select({
        "select",
        "*",
        "from comment",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_type", property="targetType", jdbcType=JdbcType.VARCHAR),
        @Result(column="storey_id", property="storeyId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_user_id", property="targetUserId", jdbcType=JdbcType.INTEGER),
        @Result(column="comment_time", property="commentTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="support_number", property="supportNumber", jdbcType=JdbcType.INTEGER),
    })
    Comment selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "*",
        "from comment"
    })
    @ResultMap("ResultMap")
    List<Comment> selectAll();

    @Select({
            "select",
            "id, comment.user_id, invitation_id,target_type, storey_id, target_user_id, comment_time, content,support_number,user_name,head_image",
            "from comment,user",
            "where comment.user_id=user.user_id"
    })
    @Results(id = "ResultViewMap",value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
            @Result(column="target_type", property="targetType", jdbcType=JdbcType.VARCHAR),
            @Result(column="storey_id", property="storeyId", jdbcType=JdbcType.INTEGER),
            @Result(column="target_user_id", property="targetUserId", jdbcType=JdbcType.INTEGER),
            @Result(column="comment_time", property="commentTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
            @Result(column="support_number", property="supportNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="head_image", property="headImage", jdbcType=JdbcType.VARCHAR)
    })
    List<CommentView> selectAllIncludeUser();

    @Select({
            "select",
            "id, comment.user_id, invitation_id,target_type, storey_id, target_user_id, comment_time, content,support_number,"
                    + "fromUser.user_name,fromUser.head_image,toUser.user_name as target_user_name",
            "from comment,user as fromUser,user as toUser",
            "where comment.user_id=fromUser.user_id and target_user_id=toUser.user_id"
    })
    @Results(id = "ResultViewMapPlus",value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
            @Result(column="target_type", property="targetType", jdbcType=JdbcType.VARCHAR),
            @Result(column="storey_id", property="storeyId", jdbcType=JdbcType.INTEGER),
            @Result(column="target_user_id", property="targetUserId", jdbcType=JdbcType.INTEGER),
            @Result(column="comment_time", property="commentTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
            @Result(column="support_number", property="supportNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="head_image", property="headImage", jdbcType=JdbcType.VARCHAR),
            @Result(column="target_user_name", property="targetUserName", jdbcType=JdbcType.VARCHAR)
    })
    List<CommentViewPlus> selectAllIncludeUserPlus();

    //查询当前帖子的评论,三表级联查询,内容更多
    @SelectProvider(type = QueryBuilder.class,method = "queryCommentViewPlusByInvitationId")
    @ResultMap("ResultViewMapPlus")
    List<CommentViewPlus> queryCommentViewPlusByInvitationId(@Param("invitationId")int invitationId);

    //查询当前帖子的评论数量,单表查询,速度更快(加.size())
    @SelectProvider(type = QueryBuilder.class,method = "commentQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Comment> commentQueryByInvitationId(@Param("invitationId")int invitationId);

    //查询当前用户评论的帖子
    @SelectProvider(type = QueryBuilder.class,method = "commentQueryByUserId")
    @ResultMap("ResultMap")
    List<Comment> commentQueryByUserId(@Param("userId")int userId);

    //查询当前用户对当前帖子是否comment
    @SelectProvider(type = QueryBuilder.class,method = "QueryMySelfComments")
    @ResultMap("ResultMap")
    List<Comment> QueryMySelfComments(@Param("invitationId")int invitationId,@Param("userId")int userId);

    //根据storeyId查询所有的评论
    @SelectProvider(type = QueryBuilder.class,method = "commentQueryByStoreyId")
    @ResultMap("ResultMap")
    List<Comment> commentQueryByStoreyId(@Param("storeyId")int storeyId);

    //查看当前帖子的当前用户的comment,用于分辨用户是否comment某个storey或者comment
    @SelectProvider(type = QueryBuilder.class,method = "myCommentIdsQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Comment>myCommentIdsQueryByInvitationId(@Param("invitationId")int invitationId,@Param("userId")int userId);

    @UpdateProvider(type = UpdateBuilder.class,method = "updateSupportNumberById")
    int updateSupportNumberById(@Param("supportNumber")int supportNumber,@Param("commentId")int commentId);

    @Update({
        "update comment",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "invitation_id = #{invitationId,jdbcType=INTEGER},",
          "storey_id = #{storeyId,jdbcType=INTEGER},",
          "target_user_id = #{targetUserId,jdbcType=INTEGER},",
          "content = #{content,jdbcType=VARCHAR}",
          "support_number = #{supportNumber,jdbcType=INTEGER},",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Comment record);
}